package com.zhxh.nzpos.setting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.zhxh.nzpos.R;
import com.zhxh.nzpos.model.Member;

public class MemberSettingAdapter extends BaseAdapter {

	private ArrayList<Member> items;
	private LayoutInflater inflater;
	ViewHolder holder;
	public static Map<Integer,ViewHolder> isSelected; 
	 
	public MemberSettingAdapter(LayoutInflater inflater, ArrayList<Member> memberItems) {
		this.items = memberItems;
		this.inflater = inflater;
		isSelected = new HashMap();
	}
	
	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		
		
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.member_setting_item, null);
			holder = new ViewHolder();
			holder.cardnumber = (TextView) convertView.findViewById(R.id.cardnumber);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.cardid = (TextView) convertView.findViewById(R.id.cardid);
			holder.phone = (TextView) convertView.findViewById(R.id.phone);
			holder.date = (TextView) convertView.findViewById(R.id.date);
			holder.points = (TextView) convertView.findViewById(R.id.points);
		   holder.cb = (CheckBox) convertView.findViewById(R.id.cb);    
			convertView.setTag(holder);
			holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					Log.i("check.............................",position+ "="+isChecked);
					if(isChecked){
						if(isSelected.isEmpty()) {
							isSelected.put(position,holder);
						}
						else {
						    
				            Iterator iterator = isSelected.keySet().iterator();                
				            while (iterator.hasNext()) {    
				             Object key = iterator.next();    
				             ViewHolder view =  isSelected.get(key);
				             holder.cb.setChecked(false);
				             
				            }           
				            
				            isSelected.clear();
				            
				            isSelected.put(position,holder);
						}
						
					}
					
				}
			});
			//convertView.seton
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
	  Member item = items.get(position);
		if (item != null) {
			holder.cardnumber.setText(item.getCardNumber());
			holder.name.setText(item.getName());
			holder.cardid.setText(item.getShfzID());
			holder.date.setText(""+item.getModified());
			holder.phone.setText(""+item.getPhone());
			holder.points.setText(""+item.getPoints());
		}
		return convertView;
	}

	static class ViewHolder {
		TextView cardnumber;
		TextView name;
		TextView cardid;
		TextView phone;
		TextView points;
		TextView date;
		CheckBox cb;
	}
}
